package tq.com.tqstudent.ui.activity.classes;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.view.View;

import com.google.gson.Gson;
import com.lib.common.base.BaseActivity;
import com.lib.common.utils.LogUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import cn.jzvd.Jzvd;
import tq.com.tqstudent.App;
import tq.com.tqstudent.R;
import tq.com.tqstudent.bean.MalistBean;
import tq.com.tqstudent.bean.params.HistoryParams;
import tq.com.tqstudent.net.ok.StringCallback;
import tq.com.tqstudent.widgets.MyJzvdStd;

/**
 * @Author rames
 * @date 2019/3/6 15:39
 */
public class VideoPlayActivity extends BaseActivity implements View.OnClickListener{
    private MyJzvdStd jzvdStd;
    private int matID;
    private int attID;
    private String title;
    private String url;

    /**
     *
     * @param activity
     * @param matID
     * @param attID
     * @param title
     * @param url
     * @param id 为了统计而加的字段,不用统计则传0
     */
    public static void show(Activity activity, int matID, int attID, String title, String url, int id) {
        Intent intent = new Intent(activity, VideoPlayActivity.class);
        Bundle bundle = new Bundle();
        bundle.putInt("matID", matID);
        bundle.putInt("attID", attID);
        bundle.putString("title", title);
        bundle.putString("url", url);
        bundle.putInt("id", id);
        intent.putExtras(bundle);
        activity.startActivity(intent);
    }

    @Override
    protected void getExtras(Bundle extras) {
        matID = extras.getInt("matID", 0);
        attID = extras.getInt("attID", 0);
        title = extras.getString("title", "");
        url = extras.getString("url", "");
    }

    @Override
    protected boolean setStatusBarTranslate() {
        return true;
    }

    @Override
    public boolean isShowTitleBar() {
        return false;
    }

    @Override
    public int initLayout() {
        return R.layout.activity_play;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        setTitle("讲评");

        findViewById(R.id.back).setOnClickListener(this);
//        String url = "http://jzvd.nathen.cn/c6e3dc12a1154626b3476d9bf3bd7266/6b56c5f0dc31428083757a45764763b0-5287d2089db37e62345123a1be272f8b.mp4";
        jzvdStd = findViewById(R.id.videoPlayer);
//        jzvdStd.setUp(App.setImgUrl(bean.content), bean.title, Jzvd.SCREEN_WINDOW_NORMAL);
        if(!TextUtils.isEmpty(url) && url.startsWith("http")) {
            jzvdStd.setUp(url, title, Jzvd.SCREEN_WINDOW_NORMAL);
            jzvdStd.startButton.performClick();
            jzvdStd.startWindowFullscreen();
        } else {
            showToast("视频地址有误");
        }
//        jzvdStd.setMediaInterface(new JZMediaIjkplayer());
//        Glide.with(this).load(App.setImgUrl(bean.content_cover)).into(jzvdStd.thumbImageView);

        recodeTimes(attID+"");
    }

    private String getPath(){
//        String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/Camera/video1.mp4";
//        String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/tencent/MicroMsg/qq/video/1.mp4";
        String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/tencent/MicroMsg/a2/video/1.mp4";

        LogUtil.e("getPath: "+path);
        return path;
    }

    private void historyRecodeTask(){
        String url = "api/clz/user/history";
        HistoryParams params = new HistoryParams();
        params.material_id = matID;
        params.attachment_id = attID;
        params.watch_sec = jzvdStd.getPosition();
        String parStr = new Gson().toJson(params);
        OkGo.<String>post(App.getHttpUrl(url))
                .upJson(parStr)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {

                    }
                });
    }

    /** 记录次数 */
    private void recodeTimes(String id){
        String url = "api/clz/ma/touch";
        String jsonStr = "{\"id\""+":"+id+"}";
        OkGo.<String>post(App.getHttpUrl(url))
                .upJson(jsonStr)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {

                    }
                });
    }

    @Override
    public void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        MyJzvdStd.goOnPlayOnResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        MyJzvdStd.goOnPlayOnPause();
    }

    @Override
    public void onBackPressed() {
        if (MyJzvdStd.backPress()) {
            this.finish();
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        jzvdStd.release();
        if(null != jzvdStd && jzvdStd.getPosition()>0 && matID!=0 && attID!=0){
            historyRecodeTask();
        }
    }

    @Override
    public void onClick(View view) {
        if(view.getId() == R.id.back){

        }
    }
}
